package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.dao.IUserInfoDao;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ConfigLowerPartnerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5528055551506251446L;

	private Logger logger = Logger.getLogger(ConfigLowerPartnerAction.class);
	
	private IConfigLowerPartnerDao configLowerPartnerDao;		//渠道信息数据接口
	private IUserInfoDao userInfoDao;							//用户信息数据接口
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息数据列表
	private List<UserInfoVo> userInfoVoList;					//用户信息数据列表
	private ConfigLowerPartnerVo configLowerPartnerVo;			//渠道信息VO
	private String actionPath;									//操作成功返回路径
	
	/**获得渠道列表*/
	public String getConfigLowerPartnerList(){
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		return SUCCESS;
	}
	/**添加渠道信息*/
	public String addConfigLowerPartner(){
		/**重置渠道信息*/
		configLowerPartnerVo = null;
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		
		return SUCCESS;
	}
	/**编辑渠道信息*/
	public String editConfigLowerPartner(){
		if(configLowerPartnerVo.getId()  != 0){
			configLowerPartnerVo = configLowerPartnerDao.getConfigLowerPartnerById(configLowerPartnerVo.getId());
		}
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		return SUCCESS;
	}
	
	/**更新渠道信息*/
	public String updateConfigLowerPartner(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		try {
			configLowerPartnerVo.setName(URLDecoder.decode(configLowerPartnerVo.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("更新渠道信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(configLowerPartnerVo.getId() == 0){
			configLowerPartnerVo.setAdd_user(userInfoVo.getId());
			/**插入渠道信息*/
			int count = configLowerPartnerDao.insertConfigLowerPartner(configLowerPartnerVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]渠道成功", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
			}else{
				logger.info(String.format("[%s]添加[%s]渠道失败", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
			}
		}else{
			configLowerPartnerVo.setUpdate_user(userInfoVo.getId());
			int count = configLowerPartnerDao.updateConfigLowerPartner(configLowerPartnerVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]渠道成功", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
			}else{
				logger.info(String.format("[%s]更新[%s]渠道失败", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
			}
		}
		
		actionPath = "configLowerPartner/getConfigLowerPartnerList.action";
		return SUCCESS;
	}
	
	/**删除渠道信息*/
	public String deleteConfigLowerPartner(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		try {
			configLowerPartnerVo.setName(URLDecoder.decode(configLowerPartnerVo.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除渠道信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = configLowerPartnerDao.deleteConfigLowerPartner(configLowerPartnerVo.getId(),userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]渠道成功", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
		}else{
			logger.info(String.format("[%s]删除[%s]渠道失败", userInfoVo.getLogin_username(),configLowerPartnerVo.getName()));
		}
		
		actionPath = "configLowerPartner/getConfigLowerPartnerList.action";
		return SUCCESS;
	}
	
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public List<UserInfoVo> getUserInfoVoList() {
		return userInfoVoList;
	}
	public void setUserInfoVoList(List<UserInfoVo> userInfoVoList) {
		this.userInfoVoList = userInfoVoList;
	}
	public ConfigLowerPartnerVo getConfigLowerPartnerVo() {
		return configLowerPartnerVo;
	}
	public void setConfigLowerPartnerVo(ConfigLowerPartnerVo configLowerPartnerVo) {
		this.configLowerPartnerVo = configLowerPartnerVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}